package com.ruoyi.web.controller.flyflow.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.controller.flyflow.vo.ProcessInstanceRecordVO;
import com.ruoyi.web.core.JoController;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 工作流 逻辑 工厂
 *
 * @author Jyl
 */
@Log4j2
@Component
public class FlyFlowFnFactory extends JoController
{
    @Autowired
    private FlyFlowFactory flyFlowFactory;

    /**
     * 获取下个节点
     */
    public String fnNextNode(JSONObject jsonObject)
    {
        if (jsonObject.getBoolean("ok"))
        {
            JSONObject json = (JSONObject) jsonObject.get("data");
            JSONArray jsonArray = json.getJSONArray("processNodeShowDtoList");

            Integer status = 2;
            String nextNodeName = "";
            for (Object o : jsonArray)
            {
                JSONObject node = (JSONObject) o;
                status = node.getInteger("status");
                //未处理状态
                if (status == 0)
                {
                    return "";
                }
                //待处理状态
                if (status == 1)
                {
                    return node.getString("name");
                }
                //递归寻找待处理状态
                nextNodeName = fnBranch(node);
                if (StrUtil.isNotEmpty(nextNodeName))
                {
                    return nextNodeName;
                }
            }
        }

        return "";
    }

    /**
     * 递归分支条件
     *
     * @param jsonObject 分支节点
     * @return 返回任务节点标识
     */
    public String fnBranch(JSONObject jsonObject)
    {
        //分支条件
        JSONArray jsonArray = jsonObject.getJSONArray("branch");
        for (Object o : jsonArray)
        {
            JSONObject node = (JSONObject) o;
            //分支子节点
            JSONArray child = node.getJSONArray("children");
            for (Object o1 : child)
            {
                //子节点
                JSONObject childNode = (JSONObject) o1;
                if (childNode.getInteger("status") == 1)
                {//待处理
                    return childNode.getString("name");
                } else if (childNode.getInteger("status") == 2)
                {//已处理
                    return fnBranch(childNode);
                } else if (childNode.getInteger("status") == 0)
                {//未处理
                    return "";
                }
            }
        }
        return "";
    }

    /**
     * 获取 流程状态 ，
     *
     * @param procId
     * @return
     */
    public Boolean isProcessEnd(String procId)
    {
        ProcessInstanceRecordVO processInstanceRecordVO = getProcessInstanceRecordVO(procId);
        if (ObjectUtil.isEmpty(processInstanceRecordVO))
        {
            return false;
        }
        log.info("[{}]:{}", "获取流程实例详情(Get process instance details)", processInstanceRecordVO);
        // 1为进行中，2为已完成
        return "1".equals(processInstanceRecordVO.getResult() + "")// 1为通过，2为不通过
               && "2".equals(processInstanceRecordVO.getStatus() + ""); //正常完成流程
    }

    /**
     * 获取 流程详情
     *
     * @param procId
     * @return
     */
    public ProcessInstanceRecordVO getProcessInstanceRecordVO(String procId)
    {
        JSONObject vo = flyFlowFactory.getByProcessInstanceId(procId);
        if (!vo.containsKey("data"))
        {
            log.error("[{}]:{}", "未获取到流程实例详情(Process instance details not obtained)", procId);
            return null;
        }

        ProcessInstanceRecordVO processInstanceRecordVO = JSONObject.parseObject(
                vo.getJSONObject("data").toJSONString(), ProcessInstanceRecordVO.class);

        log.info("[{}]:{}", "获取流程实例详情(Get process instance details)", processInstanceRecordVO);
        return processInstanceRecordVO;
    }

}
